import WorkerBase
import MSData
import MSLogger

import serial

class Worker(WorkerBase.WorkerBase):
    def __init__(self, Port, ReadTimeout=5, WriteTimeout=5):
        self.Port = Port
        self.Baudrate = 1200
        self.Bytesize = serial.EIGHTBITS
        self.Parity = serial.PARITY_NONE
        self.Stopbits = serial.STOPBITS_ONE
        self.ReadTimeout = ReadTimeout
        self.WriteTimeout = WriteTimeout
        
        self.connection = None

    def getConnectionName(self):
        return '(COM:Securithor:%s)' % (str(self.Port))

    def getConnection(self):
        return serial.Serial(port=self.Port,
                             baudrate=self.Baudrate,
                             parity=self.Parity,
                             stopbits=self.Stopbits,
                             timeout=self.ReadTimeout,
                             writeTimeout=self.WriteTimeout,
                             xonxoff=0,
                             rtscts=0)
    def isConnectionGood(self):
        return False

    def writeContactId(self, e):
        self.connection.write(e.contactIdFull)
        self.connection.write('\x14')

    def writeEventsStatic(self, MSObjects):
        self.connect()
        for obj in MSObjects:
            try:
                doneNum = 0
                obj.canClearPGSMEvents = True
                obj.eventsPGSM.sort()
                for e in obj.eventsPGSM:
                    try:
                        self.writeContactId(e)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: object(%s) PGSM event processing failed: %s' % (str(obj.objectPCNId), str(err)))
                        obj.canClearPGSMEvents = False
                        continue
                if len(obj.eventsPGSM) > 0:
                    MSLogger.logInfo('Worker::writeEventsStatic: object(%s) PGSM done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsPGSM)))
                else:
                    MSLogger.logDebug('Worker::writeEventsStatic: object(%s) PGSM done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsPGSM)))

                doneNum         = 0
                obj.canClearContactIdEvents  = True
                obj.eventsContactId.sort()
                for e in obj.eventsContactId:
                    try:
                        self.writeContactId(e)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: object(%s) ContactId event processing failed: %s' % (str(obj.objectPCNId), str(err)))
                        obj.canClearContactIdEvents = False
                        continue
                if len(obj.eventsContactId) > 0:
                    MSLogger.logInfo('Worker::writeEventsStatic: object(%s) ContactId done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsContactId)))
                else:
                    MSLogger.logDebug('Worker::writeEventsStatic: object(%s) ContactId done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsContactId)))

                doneNum         = 0
                obj.canClearBolidEvents  = True
                obj.eventsBolid.sort()
                for e in obj.eventsBolid:
                    try:
                        self.writeContactId(e)
                        doneNum += 1
                    except Exception, err:
                        MSLogger.logError('Worker::writeEventsStatic: object(%s) Bolid event processing failed: %s' % (str(obj.objectPCNId), str(err)))
                        self.canClearBolidEvents = False
                        continue
                if len(obj.eventsBolid):
                    MSLogger.logInfo('Worker::writeEventsStatic: object(%s) Bolid done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsBolid)))
                else:
                    MSLogger.logDebug('Worker::writeEventsStatic: object(%s) Bolid done(%d:%d)' % (str(obj.objectPCNId), doneNum, len(obj.eventsBolid)))



            except Exception, err:
                MSLogger.logError('Worker::writeEventsStatic: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        return


